package com.cjm.one.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.model.system.dto.SysDictDataDto;
import com.cjm.one.model.system.entity.SysDictData;
import com.cjm.one.system.mapper.SysDictDataMapper;
import com.cjm.one.system.mapper.SysDictTypeMapper;
import com.cjm.one.system.service.SysDictTypeService;
import com.cjm.one.model.system.entity.SysDictType;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 29289
* @description 针对表【sys_dict_type(字典类型表)】的数据库操作Service实现
* @createDate 2024-09-05 16:21:13
*/
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>
    implements SysDictTypeService {
    @Resource
    private SysDictTypeMapper sysDictTypeMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public List<SysDictType> findAll(SysDictDataDto sysDictDataDto) {
        LambdaQueryWrapper<SysDictType> sysDictTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (sysDictDataDto.getDictTypeKey() != null) {
            sysDictTypeLambdaQueryWrapper.like(SysDictType::getDictType, sysDictDataDto.getDictTypeKey())
                    .or().eq(SysDictType::getDictName, sysDictDataDto.getDictTypeKey());
        }
        List<SysDictType> sysDictTypes = sysDictTypeMapper.selectList(sysDictTypeLambdaQueryWrapper);
        return sysDictTypes;
    }

    @Override
    public Boolean removeDictTypeById(SysDictDataDto sysDictDataDto) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictType, sysDictDataDto.getDictTypeKey()).last("limit 1");
        SysDictData sysDictData = sysDictDataMapper.selectOne(queryWrapper);
        if (sysDictData != null) {
            throw new GlobalException(ResultCodeEnum.DELETE_DICT_ERROR);
        }
        int i = sysDictTypeMapper.deleteById(sysDictDataDto.getId());

        return i>0;
    }
}




